<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '240px'
    },
    data:{
        type: Array ,
        default:[0,0,0,0]
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  watch:{
      data: {
          deep: true,
          handler(val) {
              this.setOptions(val)
          }
      }
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.data)
    },
    setOptions(data){
        this.chart.setOption({
            tooltip: {
                trigger: 'axis',
                axisPointer: { // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
                }
            },
            legend: {
                left: 10,
                top:10,
                icon: "circle",
                itemGap:20,
                itemWidth:8,
                itemHeight:8,
                textStyle:{borderRadius:10,color:'#b6e6ff'},
                data: ['正常', '关闭', '故障'],

            },
            grid: {
                top: 60,
                left: '2%',
                right: '2%',
                bottom: 20,
                containLabel: true,
            },
            xAxis: [{
                type: 'category',
                data: ['安防', '消防', '楼控', '其他'],
                nameTextStyle:{
                    color:'#b6e6ff'
                },
                axisTick: {
                    show:false,
                    alignWithLabel: true
                },
                axisLabel:{
                    color:'#b6e6ff',
                    fontSize:14
                },
                axisTick: {
                    show: false
                },
                axisLine: {
                    show: false
                },
            }],

            yAxis: [{
                type: 'value',
                axisTick: {
                    show: false
                },
                axisLine: {
                    show: false
                },
                splitArea:{show:false},
                splitLine:{
                    show:true,
                    lineStyle:{
                        color:['rgba(255,255,255,.05)'],
                        type:'dashed'
                    }
                },
                axisLabel:{
                    color:'#559BC1',
                    fontSize:12
                }
            }],
            series: [{
                name: '正常',
                type: 'bar',
                stack: 'vistors',
                barWidth: '20%',
                data: data,
                animationDuration
            }

                // , {
                //   name: '关闭',
                //   type: 'bar',
                //   stack: 'vistors',
                //   barWidth: '20%',
                //   data: [175, 200, 330, 220],
                //   animationDuration
                // }, {
                //   name: '故障',
                //   type: 'bar',
                //   stack: 'vistors',
                //   barWidth: '20%',
                //   data: [30, 52, 70, 60],
                //   animationDuration
                // }

            ]
        })
    }
  }
}
</script>
